package com.forlove.app.qselect.index.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.index.entity.OfficialEntity;
import com.forlove.app.qselect.index.service.ISetService;
import com.forlove.app.qselect.index.service.impl.SetServiceImpl;
import com.forlove.app.qselect.index.view.SetView;
import com.forlove.app.qselect.login.entity.LoginEntity;
import com.forlove.app.qselect.update.entity.UpdateEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjl on 2017/10/30.
 */

public class SetPresenter extends IBasePresenter {
    private SetView setView;
    private ISetService setService;

    public SetPresenter(SetView setView) {
        this.setView = setView;
        if (setService == null) {
            setService = new SetServiceImpl();
            setService(setService);
        }
    }

    public void loadSendMsg(final LoginEntity loginEntity, String sign) {
        Map<String, String> param = new HashMap<>();
        param.put("mobile", loginEntity.mobile);
        param.put("sign", sign);
        setService.loadData(ServerManager.ACTION_RESETPW_MSG, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                loginEntity.parseJSONString(ParseHelper.getString(result, "data"));
                setView.showSendMsgView(loginEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    setView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    setView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");

                }
            }
        });
    }

    public void checkUpdate(String version) {
        Map map = new HashMap();
        map.put("device_type", "android");
        if (!TextUtils.isEmpty(version)) {
            map.put("app_version", version);
            LogUtil.e("yzq", version);
        }
        setService.loadData(ServerManager.ACTION_UPDATE, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                UpdateEntity entity = ParseHelper.getModel(result.toString(), "data", UpdateEntity.class);
                setView.showDownloadDialog(entity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    setView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    setView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");

                }
            }
        });
    }

    public void getOffical() {
        setService.loadData(ServerManager.ACTION_GET_OFFICIAL, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                OfficialEntity officialEntity = ParseHelper.getModel(result, "data", OfficialEntity.class);
                setView.updateOfficialData(officialEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    setView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    setView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");

                }
            }
        });
    }
}
